package com.bzkj.web.controller.ordermanagement.Inbound;

import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.domain.NccRequestDTO;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.document.domain.DocConfirmDTO;
import com.bzkj.document.domain.finishedproduct.dto.FinishedProductDTO;
import com.bzkj.document.service.finishedproductentry.FinishedProductEntryService;
import com.bzkj.document.service.finishedproductentry.FinishedProductInService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * 产成品入库单
 * @projectName: bzkj-wms
 * @package: com.bzkj.web.controller.ordermanagement
 * @className: FinishedProductEntryController
 * @description: 产成品入库单控制层
 * @author: C1309
 * @createDate: 2024年11月25日
 * @version: v1.0
 */
@RestController
@RequestMapping("/finished-products")
public class FinishedProductEntryController extends BaseController {

    @Resource
    private FinishedProductEntryService finishedProductEntryService;

    @Resource
    private FinishedProductInService finishedProductInService;

    /**
     * 获取 NCC 产成品入库单据
     *
     * @param requestBody 请求报文
     * @return 查询结果
     */
    @GetMapping("/ncc")
    @PreAuthorize("@ss.hasPermi('order:finished:list')")
    public TableDataInfo selectNccFinishedProductEntry(@RequestParam Map<String, Object> requestBody) {
        NccRequestDTO nccRequestDTO = getNccRequestDTO(requestBody);
        if (nccRequestDTO.getFbillflag() == null || nccRequestDTO.getFbillflag().isEmpty()){
            nccRequestDTO.setFbillflag("2");
        }
        return finishedProductEntryService.selectNccFinishedProductEntry(nccRequestDTO);
    }

    /**
     * 将NCC产成品入库单进行确认（插入WMS数据库）
     *
     * @return 插入结果
     */
    @PostMapping("/confirm")
    @PreAuthorize("@ss.hasPermi('order:finished:confirm')")
    @LogB(title = "产成品入库单", businessType = BusinessType.CONFIRM)
    public AjaxResult insertNccFinishedProductEntry(@RequestBody DocConfirmDTO docConfirmDTO) {
        if (docConfirmDTO == null) {
            return AjaxResult.error("您输入的参数似乎有问题，请检查并尝试重新输入哦！");
        } else if (docConfirmDTO.getvBillCodeList().isEmpty() || docConfirmDTO.getvBillCodeList() == null) {
            return AjaxResult.error("您还没有选择需要确认的单据号哦，请检查一下吧！");
        }
        docConfirmDTO.setLoginUser(getLoginUser());
        return finishedProductEntryService.insertFinishedProductEntry(docConfirmDTO);
    }

    /**
     * 产成品入库单签字
     *
     * @param cgeneralhid 表头主键
     * @return 签字结果
     */
    @PutMapping("/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:finished:audit')")
    @LogB(title = "产成品入库单", businessType = BusinessType.SIGN)
    public AjaxResult finishedProductInSignature(@PathVariable String cgeneralhid) {

        return finishedProductInService.finishedProductInSignature(cgeneralhid, getLoginUser());
    }

    /**
     * 修改成产成品入库单
     *
     * @param responseBody 产成品入库单数据
     * @return 修改成功结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('order:finished:edit')")
    @LogB(title = "产成品入库单", businessType = BusinessType.UPDATE, pkFieldNames = "cgeneralhid",
            tableNames = "wms_warehouse_in_product",
            detailTable = "wms_warehouse_in_detail_product"
    )
    public AjaxResult updateFinishedProductEntry(@RequestBody HashMap<String, Object> responseBody) {
        return finishedProductEntryService.updateFinishedProductEntry(responseBody, getLoginUser(), false);
    }

    /**
     * 获取本地已经确认的产成品入库单
     *
     * @param finishedProductDTO 查询条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('order:finished:list')")
    public TableDataInfo selectFinishedProductEntry(FinishedProductDTO finishedProductDTO) {
        if (finishedProductDTO == null) {
            finishedProductDTO = new FinishedProductDTO();
        }
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return finishedProductEntryService.selectFinishedProductEntry(finishedProductDTO, pageNum, pageSize);
    }

    /**
     * 产成品入库单删除
     *
     * @param header 表头主键
     * @param reason 删除原因
     * @return 删除结果
     */
    @DeleteMapping("/{header}")
    @PreAuthorize("@ss.hasPermi('order:finished:delete')")
    @LogB(title = "产成品入库单", businessType = BusinessType.INVALID)
    public AjaxResult finishedProductInDelete(@PathVariable String header,
                                              @RequestParam(name = "reason", defaultValue = "") String reason) {
        if (reason == null || reason.trim().isEmpty()) {
            return AjaxResult.error("作废原因似乎没有填写，请填写作废原因后重试！");
        }
        return finishedProductInService.finishedProductInDelete(header, getLoginUser(), reason);
    }

    /**
     * 产成品入库单据回退
     *
     * @param headerId 需要打回的单据主键
     * @return 回退结果
     */
    @PostMapping("/back/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:finished:edit')")
    @LogB(title = "产成品入库单", businessType = BusinessType.CANCELED)
    public AjaxResult addPurchase(@PathVariable("headerid") String headerId) {
        return finishedProductInService.orderBack(headerId, getLoginUser());
    }

    /**
     * 失败库存处理
     * @param headerId 表头主键
     * @return 处理结果
     */
    @PostMapping("/inventory/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:finished:audit')")
    @LogB(title = "产成品入库单", businessType = BusinessType.INVENTORY)
    public AjaxResult updateInventory(@PathVariable("headerid") String headerId){
        return finishedProductInService.updateInventory(headerId, getLoginUser());
    }
}